package com.mxpio.erp.technology.controller;

import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.technology.entity.BomInfoLine;
import com.mxpio.erp.common.technology.service.BomInfoLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(value = "BomInfoLineController", tags = { "Bom行接口" })
@RestController
@RequestMapping("/erp/tech/bomline/")
public class BomInfoLineController {
    @Autowired
    private BomInfoLineService bomInfoLineService;

    @GetMapping("list")
    @ApiOperation(value = "Bom行列表", notes = "获取Bom行列表（无分页）", httpMethod = "GET")
    public Result<List<BomInfoLine>> list(@ApiParam(name="查询参数") String criteria) throws UnsupportedEncodingException {
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        List<BomInfoLine> bomInfoLineList = bomInfoLineService.list(BomInfoLine.class, c);
        return Result.OK(bomInfoLineList);
    }

    @GetMapping("page")
    @ApiOperation(value = "Bom行列表", notes = "获取Bom行列表（分页）", httpMethod = "GET")
    public Result<Page<BomInfoLine>> page(@ApiParam(name="查询参数") String criteria, @ApiParam(name="每页条数")Integer pageSize, @ApiParam(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Criteria c = CriteriaUtils.json2Criteria(criteria);
        Page<BomInfoLine> bomInfoLineList = bomInfoLineService.listPage(BomInfoLine.class, page, c);
        return Result.OK(bomInfoLineList);
    }
    @GetMapping("list/{bizNo}")
    @ApiOperation(value = "根据bizNo获取Bom行", notes = "根据bizNo获取Bom行", httpMethod = "GET")
    public Result<List<BomInfoLine>> getById(@ApiParam(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<BomInfoLine> bomInfoLineList = bomInfoLineService.list(BomInfoLine.class, c);
        return Result.OK(bomInfoLineList);
    }

    @GetMapping("list/{bizNo}/{lineNo}")
    @ApiOperation(value = "根据bizNo获取Bom行", notes = "根据bizNo获取Bom行", httpMethod = "GET")
    public Result<BomInfoLine> getById(@ApiParam(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo,@ApiParam(name="行号") @PathVariable(name = "lineNo", required = true)String lineNo) {
        OrderLineKey lineKey = new OrderLineKey();
        lineKey.setLineNo(lineNo);
        lineKey.setBizNo(bizNo);
        BomInfoLine bomInfoLine = bomInfoLineService.getById(BomInfoLine.class, lineKey);
        return Result.OK(bomInfoLine);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增Bom行", notes = "新增Bom行", httpMethod = "POST")
    public Result<BomInfoLine> add(@RequestBody BomInfoLine bomInfoLine) {
        bomInfoLineService.save(bomInfoLine);
        return Result.OK(bomInfoLine);
    }

    @PutMapping("edit")
    @ApiOperation(value = "编辑Bom行", notes = "编辑Bom行", httpMethod = "PUT")
    public Result<BomInfoLine> edit(@RequestBody BomInfoLine bomInfoLine) {
        bomInfoLineService.update(bomInfoLine);
        return Result.OK(bomInfoLine);
    }

    @DeleteMapping("remove/{ids}")
    @ApiOperation(value = "根据id删除Bom行", notes = "删除Bom行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", httpMethod = "DELETE")
    public Result<BomInfoLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            bomInfoLineService.remove(BomInfoLine.class, lineKey);
        }
        return Result.OK();
    }

}
